package com.zhiwu.uhf.util;

import android.util.Log;

import com.google.gson.Gson;
import com.zhiwu.uhf.entity.BeanInfo;
import com.zhiwu.uhf.entity.ParameterInfo;

import java.util.Map;

import okhttp3.Call;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;

import static com.zhiwu.uhf.KeyData.MARK_LOCAL;
import static com.zhiwu.uhf.KeyData.MARK_UPLOAD;
import static com.zhiwu.uhf.util.Base64Util.encodeToString;

public class DataInterface {

    public Call getData(String mark, String localmark, String time, String path) {
        OkHttpClient client = new OkHttpClient();
        Gson gs = new Gson();

        ParameterInfo ParaJson = new ParameterInfo();
        ParaJson.setMark(mark);
        ParaJson.setLocalmark(localmark);
        ParaJson.setTime(time);

        String json = gs.toJson(ParaJson);

        RequestBody body = new FormBody.Builder()
                .add("data", encodeToString(json))
                .build();

        Request request = new Request.Builder()
                .url(path)
                .post(body)
                .build();
        return client.newCall(request);
    }
    public Call Upload(BeanInfo.Data data, String containers, int count, String time, String path) {
        OkHttpClient client = new OkHttpClient();
        Gson gs = new Gson();

        ParameterInfo ParaJson = new ParameterInfo();
        ParaJson.setMark(MARK_UPLOAD);
        ParaJson.setLocalmark(MARK_LOCAL);
        ParaJson.setTime(time);
        ParaJson.setDriver(data == null ? "" : data.getDriver_code());
        ParaJson.setRoute(data == null ? "" : data.getRoute_code());
        ParaJson.setCount(count + "");
        ParaJson.setContainers(containers);

        String json = gs.toJson(ParaJson);

        RequestBody body = new FormBody.Builder()
                .add("data", encodeToString(json))
                .build();
        Log.i("tag", "----Upload----"+encodeToString(json));
        Request request = new Request.Builder()
                .url(path)
                .post(body)
                .build();
        return client.newCall(request);
    }

}
